﻿using SAD_Assignment1.BLO;
using SAD_Assignment1.DTO;
using SAD_Assignment1.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SAD_Assignment1.Member
{
    public partial class MyAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["account"] == null)
            {
                Response.Redirect(Resource.Login+"?redirect="+Resource.MyAccount);
                return;
            }
            if (!IsPostBack)
            {
                initComponent();
            }
        }

        private void initComponent()
        {
            AccountDTO accDTO = (AccountDTO)Session["account"];
            txtEmail.Text = accDTO.Email;
            txtName.Text = accDTO.Name;
            txtPassword.Attributes.Add("value",accDTO.Password);
            txtConfirm.Attributes.Add("value", accDTO.Password);
        }

        protected void UpdateBtn_Command(object sender, CommandEventArgs e)
        {
            AccountDTO accDTO = (AccountDTO)Session["account"];
            AccountBLO accBLO = new AccountBLO();
            bool success = accBLO.UpdateAccount(accDTO.ID, accDTO.Email, txtName.Text, txtPassword.Text, accDTO.Roles);
            if (success)
            {
                Response.Write("<script type='text/javascript'>alert('Cập nhật tài khoản thành công');</script>");
                accDTO = accBLO.getAccountInfo(accDTO.Email);
                Session["account"] = accDTO;
                initComponent();
            }
        }
    }
}